package com.woniu113.junk.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.watch.WatchException;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu113.junk.order.client.UserClient;
import com.woniu113.junk.order.client.dto.AdminInfoDTO;
import com.woniu113.junk.order.dto.AllAppealInfoDTO;
import com.woniu113.junk.order.dto.AppealInfoDTO;
import com.woniu113.junk.order.dto.ComplaintDTO;
import com.woniu113.junk.order.dto.ComplaintInfoDTO;
import com.woniu113.junk.order.exception.AppealException;
import com.woniu113.junk.order.mapper.ComplaintInfoMapper;
import com.woniu113.junk.order.model.AppealInfo;
import com.woniu113.junk.order.mapper.AppealInfoMapper;
import com.woniu113.junk.order.model.ComplaintInfo;
import com.woniu113.junk.order.param.AddAppealInfoParam;
import com.woniu113.junk.order.param.FindAppealInfoParam;
import com.woniu113.junk.order.service.AppealInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.order.util.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class AppealInfoServiceImpl extends ServiceImpl<AppealInfoMapper, AppealInfo> implements AppealInfoService {
    @Resource
    private AppealInfoMapper aim;
    @Resource
    private UserClient userClient;
    @Resource
    private ComplaintInfoMapper complaintInfoMapper;

    @Override
    public AppealInfo find(Integer complaintId) {
        QueryWrapper<AppealInfo> queryWrapper = new QueryWrapper<>();
        if (complaintId == null) throw new AppealException("传入参数不能为空",5202);
        queryWrapper.eq("state", Data.APPEAL_STATE_NORMAL).eq("complaint_id",complaintId);
        return aim.selectOne(queryWrapper);
    }

    @Override
    public AppealInfo appeal(AddAppealInfoParam param, Integer adminId) throws Exception {
        //1.判断数据完整性
        if (param.getComplaintId()==null || param.getAppealContent()==null ||adminId==null){
            throw new Exception("请将信息填写完整");
        }
        //2.查询当前登录骑手的相关信息
        AdminInfoDTO adminInfo = userClient.selectAdminById(adminId).getData();
        //3.根据投诉id去查询是否存在该投诉id的申诉信息(一条投诉对应一条申诉信息)
        QueryWrapper<AppealInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("complaint_id",param.getComplaintId());
        AppealInfo info = aim.selectOne(queryWrapper);
        if (info!=null){//不为空表示有对应的申诉信息,不可以申诉
            throw new Exception("该投诉信息已经申诉过,不能再次申述");
        }
        //3.新建申诉信息
        AppealInfo appealInfo = new AppealInfo();
        appealInfo.setUserId(adminId);//申诉人
        appealInfo.setUserName(adminInfo.getAdminName());//申诉人姓名
        appealInfo.setUserPhone(adminInfo.getAdminPhone());//申诉人联系方式
        appealInfo.setAppealContent(param.getAppealContent());//申诉内容
        appealInfo.setComplaintId(param.getComplaintId());//投诉id
        appealInfo.setAppealTime(LocalDateTime.now());
        appealInfo.setResult(Data.APPEAL_INFO_STATE_WAITING);
        appealInfo.setState(Data.APPEAL_STATE_NORMAL);//申诉状态(正常)
        //新建
        aim.insert(appealInfo);
        return appealInfo;
    }

    @Override
    public AllAppealInfoDTO findByRider(FindAppealInfoParam param) {
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        //1.根据参数是否不为空拼接查询
        QueryWrapper<AppealInfo> queryWrapper = new QueryWrapper<>();
        if (param.getUserId() != null) {
            queryWrapper.eq("user_id", param.getUserId());
        }
        if (param.getResult() != null) {
            queryWrapper.eq("result", param.getResult());
        }
        long count = aim.selectCount(queryWrapper);
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<AppealInfo> infoList = aim.selectList(queryWrapper);
        ArrayList<AppealInfoDTO> list = new ArrayList<>();
        for (AppealInfo appealInfo : infoList) {
            AppealInfoDTO appealInfoDTO = BeanUtil.toBean(appealInfo, AppealInfoDTO.class);
            ComplaintInfo complaintInfo = complaintInfoMapper.selectById(appealInfo.getComplaintId());
            appealInfoDTO.setOrderNum(complaintInfo.getOrderNum());
            appealInfoDTO.setComplaintContent(complaintInfo.getComplaintContent());
            list.add(appealInfoDTO);
        }

        AllAppealInfoDTO dto = new AllAppealInfoDTO();
        dto.setList(list);
        dto.setTotal(Integer.valueOf((int) count));
        return dto;
    }
}
